package com.liao.badminton.service.impl;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import com.liao.badminton.code.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liao.badminton.code.myenum.RoleCodeEnum;
import com.liao.badminton.redis.RedisEnum;
import com.liao.badminton.redis.RedisUtil;
import com.liao.badminton.code.util.EntityUtils;
import com.liao.badminton.code.util.CheckUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.liao.badminton.entity.SystemRole;
import com.liao.badminton.dto.SystemRoleDto;
import com.liao.badminton.vo.SystemRoleVo;
import com.liao.badminton.mapper.SystemRoleMapper;
import com.liao.badminton.service.SystemRoleService;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Component;
import lombok.extern.log4j.Log4j2;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import static cn.hutool.core.util.ObjectUtil.isEmpty;
import static cn.hutool.core.util.ObjectUtil.isNotEmpty;
/**
 * @author liaocheng
 * @date 2024-10-30
 * description:角色信息表
 */
@Component
@Log4j2
public class SystemRoleServiceImpl  extends ServiceImpl<SystemRoleMapper, SystemRole> implements SystemRoleService {


    /**
     * 新增
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, timeout = 10, rollbackFor = Exception.class)
    public ResponseRest<Long> saveSystemRole(SystemRoleDto param) throws ProjectException{
        SystemRole systemRole = param.invertEntity();
        EntityUtils.setCreatAndUpdateInfo(systemRole);
        this.save(param.invertEntity());
        return ResponseRest.suc("saveSystemRole", systemRole.getId());
    }
    /**
     * 删除
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, timeout = 10, rollbackFor = Exception.class)
    public ResponseRest<Boolean> deletesSystemRole(SystemRoleDto param) throws ProjectException {
        boolean bool = this.removeByIds(param.getDelIds());
        if (bool) {
            param.getDelIds().forEach(id -> RedisUtil.toDelete(RedisEnum.CACHE_TABLE_DATA, "systemRole", "id", id));
        }
        return ResponseRest.suc("deletesSystemRole");
    }
    /**
     * 修改
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, timeout = 10, rollbackFor = Exception.class)
    public ResponseRest<Boolean> updateSystemRole(SystemRoleDto param)  throws ProjectException{
        SystemRole systemRole = param.invertEntity();
        EntityUtils.setUpdatedInfo(systemRole);
        boolean bool = this.updateById(systemRole);
        if (bool) {
            RedisUtil.toDelete(RedisEnum.CACHE_TABLE_DATA, "systemRole", "id", param.getId());
        }
        return ResponseRest.suc("updateSystemRole");
    }
    /**
     * 查询列表
     */
    @Override
    public ResponseRest findSystemRoleByPageParam(PageParam<SystemRoleDto> pageParam)  throws ProjectException{
        CheckUtil.checkValue(pageParam,"pageParam");
        ResponseRest resp = ResponseRest.suc("findSystemRoleByPageParam");
        PageHelper.startPage(pageParam.getCurrent(), pageParam.getSize());
        LambdaQueryWrapper<SystemRole> query = new LambdaQueryWrapper<>();
        List<SystemRole> list = this.list(query);
        List<SystemRoleVo> collect = list.stream().parallel().map(entity -> SystemRoleVo.invertToVo(entity)).collect(Collectors.toList());
        resp.setData(new PageInfo<>(collect));
        return resp;
    }
    /**
     * 查询详情
     */
    @Override
    public SystemRoleVo findDetail(SystemRoleDto param) throws ProjectException {
        SystemRoleVo vo = null;
        vo = (SystemRoleVo) RedisUtil.toGetDate(RedisEnum.CACHE_TABLE_DATA, "systemRole", "id", param.getId());
        if (isNotEmpty(vo)) {
            RedisUtil.toFlushExpire(RedisEnum.CACHE_TABLE_DATA, "systemRole", "id", param.getId());
            return vo;
        }
        SystemRole db = this.getById(param.getId());
        if (isNotEmpty(db)) {
            vo = SystemRoleVo.invertToVo(db);
            RedisUtil.toSaveRedis(RedisEnum.CACHE_TABLE_DATA,"systemRole", "id", param.getId(),vo, null);
        }
        return vo;
    }


    @Override
    public boolean hasAnySuperAdmin(Set<Long> ids) {
        if (CollectionUtil.isEmpty(ids)) {
            return false;
        }
        return ids.stream().anyMatch(id -> {
            SystemRoleDto systemRoleDto = new SystemRoleDto();
            systemRoleDto.setId(id);
            SystemRoleVo role = this.findDetail(systemRoleDto);
            return role != null && RoleCodeEnum.isSuperAdmin(role.getCode());
        });
    }

    @Override
    public List<String> findCodeByUserName(String userName) {
        return this.getBaseMapper().findCodeByUserName(userName);
    }

    @Override
    public List<SystemRole> findByUserName(String userName) {
        List<SystemRole> getDate = (List<SystemRole>)RedisUtil.toGetDate(RedisEnum.CACHE_TABLE_DATA, "systemRole", "userName", userName);
        if (isEmpty(getDate)) {
            getDate = this.getBaseMapper().findByUserName(userName);
            if (CollectionUtil.isEmpty(getDate)) {
                return null;
            }
            RedisUtil.toSaveRedis(RedisEnum.CACHE_TABLE_DATA, "systemRole", "userName", userName, getDate, null);
        }
        return getDate;
    }
}
